<?php

namespace app\modules\setting\models\seller;

use app\common\helper\CommonHelper;
use sookie\auth\models\Adminuser;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use yii\db\Expression;
use yii\helpers\ArrayHelper;
use app\modules\setting\models\seller\EtcOfficeVideoUpload;
use yii\helpers\Json;

/**
 * EtcOfficeVideoUploadSearch 是 `app\modules\setting\models\seller\EtcOfficeVideoUpload` 的搜索模型.
 */
class EtcOfficeVideoUploadSearch extends EtcOfficeVideoUpload
{
    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // 绕过scenario()在父类中的实现
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @param integer $pageSize
     *
     * @return ActiveDataProvider
     */
    public function search($params,$select=null)
    {
        $query = EtcOfficeVideoUpload::find()->select($select ? $select : "*");
        CommonHelper::notSellerThrow();
        $info = Adminuser::getUserInfo();
        $query->alias("t")->andWhere(new Expression("exists(select id from etc_office_product_selected where t.selected_id=id and seller_id={$info->id})"));

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => ['pageSize' => ArrayHelper::getValue($params,"pageSize",10),'defaultPageSize' => 10,'pageSizeParam' => 'pageSize'],
            'sort' => ['defaultOrder' => ['id' => SORT_DESC]],
        ]);

        $this->load($params,"");

        if (!$this->validate()) {
            // 如果您不想在验证失败时返回任何记录，请取消下面一行的注释
            // $query->where('0=1');
            return $dataProvider;
        }

        // 过滤条件
        $query->andFilterWhere([
            'id' => $this->id,
        ]);


                $query->andFilterWhere(['like', 'code', $this->code])                                ->andFilterWhere(['like', 'selected_id', $this->selected_id])
                                ->andFilterWhere(['like', 'user_id', $this->user_id])
                                ->andFilterWhere(['like', 'tiktok_id', $this->tiktok_id])
                                ->andFilterWhere(['like', 'video_link', $this->video_link])
                                ->andFilterWhere(['like', 'video_ad_code', $this->video_ad_code])
                                ->andFilterWhere(['like', 'video_time', $this->video_time])
                                ->andFilterWhere(['like', 'status', $this->status]);
            
        return $dataProvider;
    }
}
